package com.rfid.manager;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.rfid.a252robot.bean.Log;
import com.rfid.util.LogUtils;
import com.rfid.util.OkHttpUtils;
import com.spectre.generate.ConfigManager;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public enum  LogUploadManager {


    INSTANCE;

    Executor mExecutor = Executors.newSingleThreadExecutor();
    public void upload(Log log) {
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    String url = ConfigManager.INSTANCE.getGlobalConfig().getUploadUrl();
                    if (TextUtils.isEmpty(url)) {
                        url = "http://bjlibbot.clcn.net.cn:8899";
                    }
                    url = url + "/client/uploadLog";
                    OkHttpClient client = OkHttpUtils.getClient();
                    MediaType mediaType = MediaType.parse("application/json");
                    Gson gson = new Gson();
                    String json = gson.toJson(log);
                    LogUtils.v(json);
                    RequestBody body = RequestBody.create(mediaType, json);
                    Request request = new Request.Builder().
                            post(body)
                            .url(url)
                            .build();
                    Response response = client.newCall(request).execute();
                    json = response.body().string();
                    LogUtils.v(json);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
